
package com.help.knowhelps.ui;

	
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import com.help.knowhelps.R;
import com.help.knowhelps.commond.Utils;
import com.help.knowhelps.date.HelpInfo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class HelpAdapter extends BaseAdapter{
	private Context context;
	private ArrayList<HelpInfo> list;
	private boolean isSpecial=false;
	private ViewHolder holder;
	private String[] mShortMonths;
	private int InfoType = 0;
	private LayoutParams params;
	static public int INFOTYPE_SELF = 0; //自己提出的帮助
	static public int INFOTYPE_PARTAKE = 1;  //参与的帮助
	
	@SuppressWarnings("deprecation")
	public HelpAdapter(Context context , ArrayList<HelpInfo> list ,int InfoType) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
		this.InfoType = InfoType;
    	Configuration config = context.getResources().getConfiguration();
    	if (TextUtils.equals(config.locale.toString(), Locale.SIMPLIFIED_CHINESE.toString())  
    			|| TextUtils.equals(config.locale.toString(), Locale.CHINESE.toString())) {
    	}else {
    		mShortMonths = new String[12];
    		for(int i=0;i<12;i++){
    			mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i,DateUtils.LENGTH_MEDIUM);
    		}
		}
    	params = new LayoutParams(Utils.dip2px(context, 44), Utils.dip2px(context, 44));
    	params.setMargins(Utils.dip2px(context, 8), 0, Utils.dip2px(context, 8), 0);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return false;
	}
	@SuppressLint({ "InflateParams", "SimpleDateFormat" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HelpInfo record = list.get(position);
		if(convertView == null)
		{
    		convertView=LayoutInflater.from(context).inflate(R.layout.item_help_listview, null);
            holder = new ViewHolder();
            holder.tv_time = (TextView)convertView.findViewById(R.id.item_help_time);
            holder.tv_entity = (TextView)convertView.findViewById(R.id.item_help_entity);
            holder.tv_location = (TextView)convertView.findViewById(R.id.item_help_textView_location);
            holder.tv_count = (TextView)convertView.findViewById(R.id.item_help_textView_discuss);
            holder.icon_L =  (ImageView)convertView.findViewById(R.id.item_help_imageView_icon);
            holder.icon_R =  (ImageView)convertView.findViewById(R.id.item_help_imageView_iconRight);
            holder.ll_pictures = (LinearLayout) convertView.findViewById(R.id.item_help_pictures_layout);
            convertView.setTag(holder);
		}else {
            holder = (ViewHolder)convertView.getTag();
        }
		if(InfoType == INFOTYPE_SELF)//自己提出的帮助    
		{
			holder.icon_R.setVisibility(View.GONE);                     //设置左上角图标
			holder.icon_R.setImageBitmap(Utils.convertToBitmap(record.getRightIconSrc(),Utils.dip2px(context, 44) ,Utils.dip2px(context, 44)));  //44需适配并设置
		}else if(InfoType == INFOTYPE_PARTAKE)//����
		{
			holder.icon_R.setVisibility(View.VISIBLE);
			holder.icon_L.setImageBitmap(Utils.convertToBitmap(record.getLeftIconSrc(),Utils.dip2px(context, 44) ,Utils.dip2px(context, 44)));          //设置左上角图标
			holder.icon_R.setImageBitmap(Utils.convertToBitmap(record.getRightIconSrc(),Utils.dip2px(context, 44) ,Utils.dip2px(context, 44)));
		}
		if(null != record.getPictureList() && record.getPictureList().length != 0) //配置图片列表
		{
			holder.ll_pictures.setVisibility(View.VISIBLE);
			for(String picture : record.getPictureList())
			{
				ImageView iv_temp = new ImageView(context);
				iv_temp.setImageBitmap(Utils.convertToBitmap(picture,Utils.dip2px(context, 44) ,Utils.dip2px(context, 44)));
				holder.ll_pictures.addView(iv_temp, params);
			}
		}else
		{
			holder.ll_pictures.setVisibility(View.GONE);
		}
		try{
			holder.tv_time.setText(record.getTime());  //是否需要多语言检测?
			if(holder.tv_entity != null) {
				holder.tv_entity.setText(record.getEntity());
			}
			holder.tv_location.setText(record.getLocation());
			holder.tv_count.setText(record.getDiscussCount()+"");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
        return convertView;
	}
	private static class ViewHolder {
	    private TextView tv_time,tv_entity,tv_location,tv_count;
	    private ImageView icon_L,icon_R;
	    private LinearLayout ll_pictures;
	}
}


